package stdio.costa.imoca.exception;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Provider
@Repository
public class AuthenticationExceptionMapper implements
		ExceptionMapper<AuthenticationFailedException> {

	private static Logger log = LoggerFactory
			.getLogger(AuthenticationExceptionMapper.class);

	@Override
	public Response toResponse(AuthenticationFailedException e) {
		log.error(Status.UNAUTHORIZED.toString(), e);

		ErrorResponse error = new ErrorResponse(
				ErrorCodeConstant.ERROR_CODE_INVALID_SECURITY, e.getMessage());

		return Response.status(Status.UNAUTHORIZED)
				.type(MediaType.APPLICATION_JSON).entity(error).build();

	}
}
